<?php class FckHelper extends Helper {
    
    var $helpers = Array('Html');
    var $toolbar = 'Custom';
    function load($id, $toolbar = 'Default', $width = '100%', $height = '200') {
        $did = '';
        foreach (explode('.', $id) as $v) {
             $did .= ucfirst($v);
        }
        
        $basePath = '/js/fckeditor/';
        $basePath = $this->Html->url($basePath);
        
        return <<<FCK_CODE
<script type="text/javascript">
fckLoader_{$did} = function () {
    var bFCKeditor_{$did} = new FCKeditor('{$did}');
    bFCKeditor_{$did}.BasePath = '{$basePath}';
    bFCKeditor_{$did}.ToolbarSet = '{$toolbar}';
    bFCKeditor_{$did}.Width = '{$width}';
    bFCKeditor_{$did}.Height = '{$height}';
    bFCKeditor_{$did}.ReplaceTextarea();
}
setTimeout("fckLoader_{$did}()",2000);
</script>
FCK_CODE;
    }
    function fckeditorjs($namepair, $content="", $width="100%", $height="100%"){
        $editor_id = "";
        
        $content=preg_replace("/\n/","",$content);
		$content=preg_replace("/\r/","",$content);
		$content=preg_replace("/'/","\\\'",$content);
		
        if(is_array($namepair)) {
	        foreach ($namepair as $name){
	            $editor_id .= ucfirst($name);
	        }	
        }else {
        	$editor_id = Inflector::camelize(str_replace('.', '_', $namepair)); 
        }
        
		return "<script type=\"text/javascript\">
					fckLoader_".$editor_id." = function () {
					    var bFCKeditor_$editor_id = new FCKeditor('$editor_id');
					    bFCKeditor_".$editor_id.".BasePath = '".$this->webroot."js/fckeditor/';
					    bFCKeditor_".$editor_id.".Value = '$content';
					    bFCKeditor_".$editor_id.".ToolbarSet = '".$this->toolbar."';
					    bFCKeditor_".$editor_id.".Config['ToolbarStartExpanded'] = true ;
					    bFCKeditor_".$editor_id.".Width = '".$width."';
    					bFCKeditor_".$editor_id.".Height = '".$height."';
					    bFCKeditor_".$editor_id.".ReplaceTextarea();
					}
					fckLoader_$editor_id();
				</script>";
        
    } 
}
?> 